unit platform;

interface

procedure bson_swap_endian64(var o; const i); inline;
procedure bson_swap_endian32(var o; const i); inline;

procedure bson_little_endian64(var o; const i); inline;
procedure bson_little_endian32(var o; const i); inline;
procedure bson_big_endian64(var o; const i); inline;
procedure bson_big_endian32(var o; const i); inline;

implementation

procedure bson_swap_endian64(var o; const i); inline;
var
  po, pi: PByte;
begin
  po := PByte(@o);
  pi := PByte(@i);
  po[0] := pi[7];
  po[1] := pi[6];
  po[2] := pi[5];
  po[3] := pi[4];
  po[4] := pi[3];
  po[5] := pi[2];
  po[6] := pi[1];
  po[7] := pi[0];
end;

procedure bson_swap_endian32(var o; const i); inline;
var
  po, pi: PByte;
begin
  po := PByte(@o);
  pi := PByte(@i);
  po[0] := pi[3];
  po[1] := pi[2];
  po[2] := pi[1];
  po[3] := pi[0];
end;

procedure bson_little_endian64(var o; const i); inline;
begin
  Move(i, o, 8);
end;

procedure bson_little_endian32(var o; const i); inline;
begin
  Move(i, o, 4);
end;

procedure bson_big_endian64(var o; const i); inline;
begin
  bson_swap_endian64(o, i);
end;

procedure bson_big_endian32(var o; const i); inline;
begin
  bson_swap_endian32(o, i);
end;

end.
